package cn.lyjuan.archetypes.bms.web.controller;


import cn.lyjuan.archetypes.bms.common.entity.MgManage;
import cn.lyjuan.archetypes.bms.web.bean.vo.BaseResp;
import cn.lyjuan.archetypes.bms.web.service.RoleService;
import cn.lyjuan.archetypes.bms.web.service.ActionService;
import cn.lyjuan.archetypes.bms.web.bean.vo.PagerResp;
import cn.lyjuan.archetypes.bms.web.controller.base.SecurityController;
import cn.lyjuan.archetypes.bms.web.cst.StateConst;
import cn.lyjuan.archetypes.bms.common.entity.MgAction;
import cn.lyjuan.archetypes.bms.common.entity.MgActionGroup;
import cn.lyjuan.archetypes.bms.web.service.ActionGroupService;
import cn.lyjuan.archetypes.bms.web.service.ManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ly on 2015/1/16.
 */
@Controller
@RequestMapping("action/")
public class ActionController extends SecurityController
{
    @Autowired
    @Qualifier("mgActionService")
    private ActionService actionSer;

    @Autowired
    @Qualifier("mgRoleService")
    private RoleService roleSer;

    @Autowired
    @Qualifier("mgManageService")
    private ManageService manageSer;

    @Autowired
    @Qualifier("mgActionGroupService")
    private ActionGroupService actionGroupSer;

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public ModelAndView toList()
    {
        return mv("/action/action_list");
    }

    /**
     * 分页检索请求信息
     *
     * @param groupid
     * @param model
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public PagerResp<MgAction> list(
            @RequestParam(required = false) Integer groupid,
            @RequestParam(defaultValue = "1") Integer pn,
            @RequestParam(required = false) Integer ps, Model model)
    {
        ps = null == ps ? this.getPs() : ps;
        MgManage manage = manageSer.queryById(this.getUserId());

        Map<String, String> param = this.paramsToMap();

        List<MgAction> list = actionSer.queryActionsForManage(manage, param, pn, ps);
        model.addAttribute(BaseResp.resp(list));// 需要封装分页信息

//        if (null != groupid)
//        {
//            MgActionGroup group = actionGroupSer.queryGroupById(groupid);
//            model.addAttribute("group", group);
//        }

        return BaseResp.resp(list);
    }

    /**
     * 添加请求页面
     *
     * @return
     */
    @RequestMapping(value = "add", method = RequestMethod.GET)
    public ModelAndView toAdd(Integer groupid, Model model)
    {
        if (null != groupid && groupid > 0)
        {
            MgActionGroup group = actionGroupSer.queryGroupById(groupid);
            model.addAttribute("group", group);
        }

        // 查询所有分组
        List<MgActionGroup> groups = actionGroupSer.queryAllGropus();

        Map<String, Object> map = new HashMap<>(2);
        map.put("groups", groups);
        model.addAttribute("data", map);

        return mv("/action/action_edit");
    }

    /**
     * 添加请求
     *
     * @return
     */
    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    public BaseResp add(MgAction action)
    {
        actionSer.addAction(action);

        return BaseResp.resp();
    }

    /**
     * 修改请求页面
     *
     * @return
     */
    @RequestMapping(value = "edit", method = RequestMethod.GET)
    public ModelAndView toEdit(Integer id, Model model)
    {
        // 请求
        MgAction action = actionSer.queryActionById(id);

        // 请求组
        // 查询所有分组
        List<MgActionGroup> groups = actionGroupSer.queryAllGropus();

        Map<String, Object> map = new HashMap<>(2);
        map.put("data", action);
        map.put("groups", groups);
        model.addAttribute("data", map);

        return mv("/action/action_edit");
    }

    /**
     * 修改请求
     *
     * @return
     */
    @RequestMapping(value = "edit", method = RequestMethod.POST)
    @ResponseBody
    public BaseResp edit(MgAction action)
    {
        actionSer.updateAction(action);

        return BaseResp.resp();
    }

    /**
     * 更新请求状态
     *
     * @return
     */
    @RequestMapping("status")
    @ResponseBody
    public BaseResp status(Integer id, Boolean status)
    {
        actionSer.updateStatus(id, status ? StateConst.OK : StateConst.NO);

        return BaseResp.resp();
    }
}
